/* Sokoban
 * HIT3243
 * Authors: Mike Blackney, Ben Ingarfield, Kurniawan
 */

#include <fstream>
#include "options.h"
#include "registrar.h"

// Here are the GUI magic numbers:

#define IDC_DIFFICULTY		1
#define IDC_OKAY			8
#define IDC_CANCEL			9

// The options file name:
#define OPTIONS_FILE L"sokoban_options.txt"

Options::Options() {
	// The first time an Options is created, load
	// the options from the saved file.

	/*
	// I ran out of time to finish implementing and testing
	// loading and saving of options.  It would be trivial,
	// though, and most of the code is here:

	// Now load the file:
	HRESULT hr;
	WCHAR strPath[MAX_PATH];
	hr = DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, OPTIONS_FILE );
	if(FAILED(hr)) {
		// We just set the options to defaults:
	}
	else {
		// If the options file *is* found, load it:
		std::wifstream options_file(strPath);
		assert(options_file);

		//options_file >> BLAH;
		
		options_file.close( );
	}
	*/
	
}

Options::~Options( ) {
	/*
	// More load/save that is unfinished:
// The destructor saves our options to file:
	WCHAR strPath[MAX_PATH];
	DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, OPTIONS_FILE );
	
	std::wofstream options_file(strPath);
	assert(options_file);

	// Go to the start of the file - we don't want to keep old optionss:
	options_file.seekp(0);

	//options_file << BLAH;

	options_file.close( );
	*/
}

Options* const Options::Instance() {
	static Options instance;
	return &instance;
}

HRESULT Options::Load() {
	HRESULT hr;

	V_RETURN(GameStateGUI::Load());

	// Load the fonts:
	V_RETURN(largefont.Load(L"Tahoma", 24, FW_NORMAL));
	V_RETURN(smallfont.Load(L"Tahoma", 12, FW_NORMAL));

	// Get the title sprite:
	const D3DSURFACE_DESC* d = DXUTGetBackBufferSurfaceDesc( );
	V_RETURN(title.CreateFromFile(L"s_options.jpg"));
	title.SetPosition( d->Width / 2 - title.GetWidth( ) / 2, d->Height / 4 - title.GetHeight( ) / 2 );

	// Add the GUI items
	//const D3DSURFACE_DESC* d = DXUTGetBackBufferSurfaceDesc();
	V_RETURN(dialog.AddComboBox(IDC_DIFFICULTY, 150, 325, 150, 25, L'G', false, &pDifficulty));
    if(pDifficulty) {
        pDifficulty->SetDropHeight( 50 );
        pDifficulty->AddItem( L"Novice", (LPVOID*)0);
        pDifficulty->AddItem( L"Intermediate", (LPVOID*)1);
        pDifficulty->AddItem( L"Advanced", (LPVOID*)2);
		pDifficulty->SetSelectedByData((LPVOID*)Registrar::iDifficulty );
		iDifficulty = Registrar::iDifficulty;
	}

    /*
	V_RETURN(dialog.AddComboBox(IDC_SPEED, 500, 325, 200, 25, L'S', false, &pAdvOptions[0]));
    if(pAdvOptions[0]) {
        pAdvOptions[0]->SetDropHeight( 50 );
        pAdvOptions[0]->AddItem( L"Short", (LPVOID*)0);
        pAdvOptions[0]->AddItem( L"Medium", (LPVOID*)1);
        pAdvOptions[0]->AddItem( L"Long", (LPVOID*)2);
        pAdvOptions[0]->AddItem( L"Painful", (LPVOID*)3);
		pAdvOptions[0]->SetSelectedByData((LPVOID*)Registrar::iAdvSpeed );
		pAdvOptions[0]->SetEnabled(false);
		iAdvSpeed = Registrar::iAdvSpeed;
	}
	*/

	/*
    V_RETURN(dialog.AddComboBox(IDC_RESPAWN, 500, 350, 200, 25, L'R', false, &pAdvOptions[1]));
    if(pAdvOptions[1]) {
        pAdvOptions[1]->SetDropHeight( 50 );
        pAdvOptions[1]->AddItem( L"Occasional", (LPVOID*)0);
        pAdvOptions[1]->AddItem( L"Regular", (LPVOID*)1);
        pAdvOptions[1]->AddItem( L"Frequent", (LPVOID*)2);
        pAdvOptions[1]->AddItem( L"Nonstop!", (LPVOID*)3);
		pAdvOptions[1]->SetSelectedByData((LPVOID*) Registrar::iAdvRespawn);
		pAdvOptions[1]->SetEnabled(false);
		iAdvRespawn = Registrar::iAdvRespawn;
	}
	*/

    /*
	V_RETURN(dialog.AddComboBox(IDC_LIVES, 500, 375, 200, 25, L'L', false, &pAdvOptions[2]));
    if(pAdvOptions[2]) {
        pAdvOptions[2]->SetDropHeight( 50 );
        pAdvOptions[2]->AddItem( L"5", (LPVOID*)0);
        pAdvOptions[2]->AddItem( L"3", (LPVOID*)1);
        pAdvOptions[2]->AddItem( L"1", (LPVOID*)2);
		pAdvOptions[2]->SetSelectedByData((LPVOID*) Registrar::iAdvLives);
		pAdvOptions[2]->SetEnabled(false);
		iAdvLives = Registrar::iAdvLives;
	}
	*/

	V_RETURN(dialog.AddButton(IDC_OKAY, L"Okay", 450, 450, 100, 25, 'O', true));
	V_RETURN(dialog.AddButton(IDC_CANCEL, L"Cancel", 550, 450, 100, 25, 'C'));

	// Enable mouse
	DXUTSetCursorSettings(true, false);
	DXUTGetD3DDevice()->ShowCursor(true);

	dxf::DXFSetClear(true);
	dxf::DXFSetClearColor(BLACK);
	return S_OK;
}

void Options::Unload() {
	largefont.Unload( );
	smallfont.Unload( );
	title.Unload( );

	// If we've pressed Okay, update the values in Registrar:
	if (bUpdateOptions) {
		Registrar::sGameType = std::wstring(L"New Game: ")
			+ std::wstring(pDifficulty->GetSelectedItem()->strText);
		Registrar::iDifficulty = iDifficulty;
		//Registrar::iAdvSpeed = iAdvSpeed;
		//Registrar::iAdvRespawn = iAdvRespawn;
		//Registrar::iAdvLives = iAdvLives;
	}
	GameStateGUI::Unload();
	
}

void Options::Update(double fTime, float fElapsedTime) {
}

void Options::Render2D(double fTime, float fElapsedTime) {
	// Render the "Options" heading sprite:

	// Render the GUI info text:
	largefont.Render2D( L"Game Type", D3DXVECTOR2(150,300));

	title.Render2D( );
}

void Options::Resize(const D3DSURFACE_DESC* d) {
	// Put the dialog back in the corner (this step probably unnecessary)
	dialog.SetLocation( 0, 0 );

	// Make its size match the size of the screen
	dialog.SetSize( d->Width, d->Height );
}

bool Options::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	// Pass windows messages to the resource manager and all the gui objects
	if (GameStateGUI::MsgProc(hWnd, uMsg, wParam, lParam)) return true;

	// Pressing ESC during score entry will take you to the Title:
	if (uMsg == WM_KEYDOWN && wParam == VK_ESCAPE) {
		bUpdateOptions = false;
		dxf::DXFChangeState(Registrar::kTitle);
		return true;
	}

	return false;	// let the engine handle everything else.
}

void Options::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) {
    switch( nControlID ) {

	case IDC_DIFFICULTY: {

		// ---DOES NOTHING NOW---
		/*
		iDifficulty = (long long)(((CDXUTComboBox*)pControl)->GetSelectedItem()->pData);

		bool custom = (iDifficulty == 3);

		for (int i = 0; i < 3; ++i) {
			pAdvOptions[i]->SetEnabled(custom);

			if (!custom) {
				// All non-custom games are the same speed:
				if (i == 0)
					pAdvOptions[i]->SetSelectedByData((LPVOID*)1);
				// But differ by respawn and lives:
				else 
					pAdvOptions[i]->SetSelectedByData((LPVOID*)iDifficulty);
			}
		}

		if (!custom) {
			iAdvSpeed = iAdvRespawn = iAdvLives = iDifficulty;
		}
		*/

		break;
		}

	/*
	case IDC_SPEED:
		iAdvSpeed = (long long)(((CDXUTComboBox*)pControl)->GetSelectedItem()->pData);
		break;

	case IDC_RESPAWN:
		iAdvRespawn = (long long)(((CDXUTComboBox*)pControl)->GetSelectedItem()->pData);
		break;

	case IDC_LIVES:
		iAdvLives = (long long)(((CDXUTComboBox*)pControl)->GetSelectedItem()->pData);
		break;
	*/

	case IDC_OKAY: 
		bUpdateOptions = true;
		dxf::DXFChangeState(Registrar::kTitle);
		break;

	case IDC_CANCEL:
		bUpdateOptions = false;
		dxf::DXFChangeState(Registrar::kTitle);
		break;

	default:
		assert(false);
		break;
	}
}